package com.Zhengxin.smarthome.db;

import java.util.ArrayList;

import com.Zhengxin.bean.AlarmDB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseHelper {

	private SQLiteDatabase db;
	private DatabaseOpenHelper helper;
	private Cursor c;
	public DatabaseHelper(Context context){
		helper = new DatabaseOpenHelper(context);
		db = helper.getWritableDatabase();
	}
	
	public void insertAlarm(AlarmDB alarmDB){
		ContentValues cv = new ContentValues();
		cv.put("Time", alarmDB.getTime());
		cv.put("Week", alarmDB.getWeek());
		cv.put("Equipment", alarmDB.getEquipment());
		db.insert(DatabaseOpenHelper.alarmTable, null, cv);
	}
	
	public void updateAlarm(AlarmDB alarmDB){
		ContentValues cv = new ContentValues();
		cv.put("Time", alarmDB.getTime());
		cv.put("Week", alarmDB.getWeek());
		cv.put("Equipment", alarmDB.getEquipment());
		db.update(DatabaseOpenHelper.alarmTable, cv,"ID="+alarmDB.getId() , null);
	}
	
	public void deleteAlarm(AlarmDB alarmDB){
		db.delete(DatabaseOpenHelper.alarmTable,"ID="+alarmDB.getId(), null);
	}
	
	public ArrayList<AlarmDB> queryAlarm(String where){
		ArrayList<AlarmDB> list = new ArrayList<AlarmDB>();
		c = db.query(DatabaseOpenHelper.alarmTable, null, where, null, null, null, null);
		if (c.moveToFirst()){
			do {
				AlarmDB a = new AlarmDB(c.getInt(c.getColumnIndex("ID")), c.getString(c.getColumnIndex("Time")), c.getString(c.getColumnIndex("Week")), c.getString(c.getColumnIndex("Equipment")));
				list.add(a);
			}while(c.moveToNext());
		}
		c.close();
		return list;
	}
	public void dbClose(){
		db.close();
		//c.close();
	}
}
